const express = require('express')
const db = require('../01-封装函数db')
const router = express.Router()

//获取分类列表数据接口
router.get('/list', (req, res) => {
    db.db('select * from category', (err, result) => {
        if (err) throw err
        res.send({ status: 0, message: '获取分类列表信息成功', data: result })
    })
})

//添加分类接口
router.post('/add', (req, res) => {
    let { name, alias } = req.body
    // console.log(req.body);
    db.db(`insert into category set name='${name}',alias='${alias}'`, (err, result) => {
        if (err) throw err
        res.send({ status: 0, message: '添加分类成功' })
    })
})

//删除分类接口
router.get('/delete', (req, res) => {
    let id = req.query.id
    // console.log(req.query);
    db.db(`delete from category where id='${id}'`, (err, result) => {
        if (err) throw err
        res.send({ status: 0, message: '删除数据成功' })
    })
})

//修改分类接口
router.post('/update', (req, res) => {
    let { name, alias, id } = req.body
    db.db(`update category set name='${name}',alias='${alias}' where id ='${id}'`, (err, result) => {
        if (err) throw err
        // console.log(result.affectedRows);  //操作所影响的记录行数
        if (result.affectedRows > 0) {
            res.send({ status: 0, message: '修改分类成功' })
        } else {
            res.send({ status: 1, message: '修改分类失败' })
        }
    })
})

module.exports = router